//
//  MyMessageViewController.m
//  LiuHeBao
//
//  Created by zr on 2018/8/27.
//  Copyright © 2018年 zr. All rights reserved.
//

#import "MyMessageViewController.h"
#import "MyCollectionTableViewCell.h"
#import "MyMessageModel.h"
#import "WishInfoViewController.h"

@interface MyMessageViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, weak)UITableView * tableView;
@property (nonatomic, strong)NSArray<MyMessageModel *> * modelArray;

@end

@implementation MyMessageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = WHITE_COLOR;
    
    [MyMessageModel myMessageModelWithSucess:^(NSArray<MyMessageModel *> *array) {
        
        self.modelArray = array;
    } error:^{
        
    }];
    
    [self setupNav];
    [self setupUI];
}

- (void)setModelArray:(NSArray<MyMessageModel *> *)modelArray{
    
    _modelArray = modelArray;
    
    [self.tableView reloadData];
}

- (void)setupNav{
    
    self.title = @"我的消息";
    
    UIButton * backButton = [[UIButton alloc] init];
    
    backButton.frame = CGRectMake(0, 0, 12 * SCREENH6, 20 * SCREENH6);
    [backButton setImage:[UIImage imageNamed:@"1返回"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backToView) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem * leftButton = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    
    self.navigationItem.leftBarButtonItem = leftButton;
    
}


- (void)setupUI{
    
    UITableView * tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREENH_HEIGHT) style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.backgroundColor = [UIColor whiteColor];
    tableView.rowHeight = 80 * SCREENH6;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.tableView = tableView;
    
    [self.tableView registerClass:[MyCollectionTableViewCell class] forCellReuseIdentifier:@"MyMessageListCell"];
    
    [self.view addSubview:self.tableView];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    return self.modelArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    MyCollectionTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyMessageListCell" forIndexPath:indexPath];
    
    cell.msgModel = self.modelArray[indexPath.row];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    HIDDEN_TAB;
    
    MyMessageModel * model = self.modelArray[indexPath.row];
    
    WishInfoViewController * infoVC = [WishInfoViewController new];
    
    infoVC.ID = model.ID;
    
    [self.navigationController pushViewController:infoVC animated:YES];
    
}


- (void)backToView {
    
    [self.navigationController popViewControllerAnimated:YES];
}

@end
